package com.book.service.impl;

import com.book.entity.*;
import com.book.mapper.*;
import com.book.service.AddressService;
import com.book.vo.AddressVo;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @program:
 * @ClassName:
 * @description:
 * @author: zgc
 * @date:
 * @Version 1.0
 **/
@Service
public class AddressServiceImpl implements AddressService {
    @Resource
    private BookUserAddressMapper bookUserAddressMapper;

    @Resource
    private ProvincesMapper provincesMapper;

    @Resource
    private CitiesMapper citiesMapper;

    @Resource
    private AreasMapper areasMapper;

    @Resource
    private StreetsMapper streetsMapper;

    @Resource
    private VillagesMapper villagesMapper;

    @Override
    public List<AddressVo> findAddressByUserId(Integer userId) {
        return bookUserAddressMapper.selectAddressByUserId(userId);
    }

    @Override
    public int selectDefaultAddressByUserId(Integer userId) {
        Example example = new Example(BookUserAddress.class);
        example.createCriteria().andEqualTo("accountId",userId)
                .andEqualTo("isDefault",1);
        bookUserAddressMapper.selectOneByExample(example);
        return 0;
    }

    
    @Override
    public int addAddressByUserId(BookUserAddress bookUserAddress) throws Exception {
        bookUserAddress.setIsDefault((byte) 0);
        bookUserAddress.setIsDelete((byte) 0);
        bookUserAddress.setCreateTime(new Date());
        int insert = bookUserAddressMapper.insert(bookUserAddress);
        return insert;
    }

    @Override
    public int selectAddressCountByUserId(Long userId) throws Exception {
        int i = bookUserAddressMapper.selectAddressCountByUserId(userId);
        return i;
    }

    @Override
    //@Cacheable(value = "address",key="#root.methodName+#root.args")
    public List<Provinces> findProvincesByCode() throws Exception {
        return provincesMapper.selectAll();
    }

    /**
     * 根据省市的code查询城市的地址
     */
    @Override
    //@Cacheable(value = "address",key="#root.methodName+#root.args")
    public List<Cities> findCitiesByProvinceCode(String provinceCode) throws Exception {
        Example example = new Example(Cities.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("provinceCode", provinceCode);
        return citiesMapper.selectByExample(example);
    }

    /**
     * 根据城市的code查询区县的地址
     */
    @Override
   // @Cacheable(value = "address",key="#root.methodName+#root.args")
    public List<Areas> findAreasByCityCode(String cityCode) throws Exception {
        Example example = new Example(Areas.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("cityCode", cityCode);
        return areasMapper.selectByExample(example);
    }

    /**
     * 根据区县的code查询街道的地址
     */
    @Override
    //@Cacheable(value = "address",key="#root.methodName+#root.args")
    public List<Streets> findStreetsByAreaCode(String areaCode) throws Exception {
        Example example = new Example(Streets.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("areaCode", areaCode);
        return streetsMapper.selectByExample(example);
    }

    /**
     * 根据街道的code查询居委的地址
     */
    @Override
   // @Cacheable(value = "address",key="#root.methodName+#root.args")
    public List<Villages> findVillagesByStreetCode(String streetCode) throws Exception {
        Example example = new Example(Villages.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("streetCode", streetCode);
        return villagesMapper.selectByExample(example);
    }
    
    
}
